package com.example.democontactperson;

import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.ArrayMap;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final ExpandableListView listView = findViewById(R.id.expandable);
        listView.setGroupIndicator(null);


        ArrayMap<Character, String[]> arrayMap = initData();
        MyAdapter myAdapter = new MyAdapter(arrayMap);
        listView.setAdapter(myAdapter);

        Character[] characters = new Character[arrayMap.size()];
        arrayMap.keySet().toArray(characters);

        final ContactView<Character> contactView = findViewById(R.id.contactview);
        contactView.setCharacters(characters);
        contactView.setTextSize(40);
        contactView.setTextColor(Color.DKGRAY);
        contactView.setSelectBackGroundColor(Color.GRAY);
        contactView.setOnScrollTouchListener(new ContactView.OnScrollTouchListener<Character>() {
            @Override
            public void onScroll(ContactView view, int position, int count, Character c) {
                listView.setSelectedGroup(position);
            }
        });

        listView.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {

            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
                final long packedPosition = listView.getExpandableListPosition(firstVisibleItem);
                final int groupPosition = ExpandableListView.getPackedPositionGroup(packedPosition);
                contactView.setSelectPosition(groupPosition);

            }
        });
    }

    private ArrayMap<Character, String[]> initData() {
        ArrayMap<Character, String[]> arrayMap = new ArrayMap<>();
        for (char i = 65; i <= 90; ++i) {
            String[] strings = new String[10];
            StringBuilder builder = new StringBuilder();
            builder.append(i);
            for (int z = 0; z < strings.length; ++z) {
                builder.append(z);
                strings[z] = builder.toString();
                builder.deleteCharAt(builder.length() - 1);
            }
            arrayMap.put(i, strings);
        }
        return arrayMap;
    }


    private static class MyAdapter extends BaseExpandableListAdapter {

        final ArrayMap<Character, String[]> mData;

        MyAdapter(ArrayMap<Character, String[]> data) {
            mData = data;
        }


        @Override
        public int getGroupCount() {
            return mData.size();
        }

        @Override
        public int getChildrenCount(int groupPosition) {
            return mData.valueAt(groupPosition).length;
        }

        @Override
        public Character getGroup(int groupPosition) {
            return mData.keyAt(groupPosition);
        }

        @Override
        public String getChild(int groupPosition, int childPosition) {
            return mData.valueAt(groupPosition)[childPosition];
        }

        @Override
        public long getGroupId(int groupPosition) {
            return 0;
        }

        @Override
        public long getChildId(int groupPosition, int childPosition) {
            return 0;
        }

        @Override
        public boolean hasStableIds() {
            return false;
        }

        @Override
        public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
            TextView textView = null;
            if ((textView = (TextView) convertView) == null) {
                textView = new TextView(parent.getContext());
                textView.setBackgroundColor(Color.GRAY);
                textView.setTextSize(20);
            }
            textView.setText(getGroup(groupPosition).toString());
            if (!isExpanded) {
                final ExpandableListView listView = (ExpandableListView) parent;
                listView.expandGroup(groupPosition);
            }


            return textView;
        }

        @Override
        public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
            TextView textView = null;
            if ((textView = (TextView) convertView) == null) {
                textView = new TextView(parent.getContext());
            }
            textView.setText(getChild(groupPosition, childPosition));
            return textView;
        }


        @Override
        public boolean isChildSelectable(int groupPosition, int childPosition) {
            return true;
        }
    }
}
